/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.minierp.bean.OrderParameterCarrier;
import com.minierp.bean.QueryParameterCarrier;
import com.minierp.bean.QueryResult;
import com.minierp.bean.UserBean;
import com.minierp.bean.UserStatus;
import com.minierp.dao.AccessoryDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.MessageDao;
import com.minierp.dao.QueryDao;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.EncInterceptorHelper;
import com.minierp.model.Message;
import com.minierp.model.MessageReceiver;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class MessageMACtr extends MultiActionController {
	private QueryDao queryDao;
	private MessageDao messageDao;
	private AccessoryDao accessoryDao;
	private IdGenerator idGenerator;
	private String listView;
	private String infoView;
	private String operateSuccessView;
	private String mainView;
	private String menuView;

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public String getMainView() {
		return this.mainView;
	}

	public void setMainView(String mainView) {
		this.mainView = mainView;
	}

	public String getMenuView() {
		return this.menuView;
	}

	public void setMenuView(String menuView) {
		this.menuView = menuView;
	}

	public ModelAndView handleList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.listView);
		String type = request.getParameter("type");
		String relationId = request.getParameter("relation_id");
		String searchType = request.getParameter("search_type");
		String ttl = request.getParameter("ttl");
		String con = request.getParameter("con");
		String sen = request.getParameter("sen");
		String startDate = request.getParameter("start_date");
		String endDate = request.getParameter("end_date");
		String objName = "com.minierp.model.Message";
		String objShortName = "b";
		HashMap shortNameMap = new HashMap();
		String[] retObjs = new String[]{objShortName};
		Vector qpVe = new Vector();
		shortNameMap.put(objName, objShortName);
		HashMap parameters = new HashMap();
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		QueryParameterCarrier qp;
		String qParas;
		String p1;
		if (type != null && type.equals("IN")) {
			qParas = "com.minierp.model.MessageReceiver";
			p1 = "r";
			shortNameMap.put(qParas, p1);
			retObjs = new String[]{objShortName, p1};
			qp = new QueryParameterCarrier();
			qp.objectName = objName;
			qp.propertyName = "id";
			qp.value = "b.id=r.messageId";
			qp.dataType = 6;
			qp.logicType = 10;
			qpVe.add(qp);
			qp = new QueryParameterCarrier();
			qp.objectName = qParas;
			qp.propertyName = "receiverId";
			qp.value = String.valueOf(ub.getStaffer().getId());
			qp.dataType = 3;
			qp.logicType = 1;
			qpVe.add(qp);
			qp = new QueryParameterCarrier();
			qp.objectName = qParas;
			qp.propertyName = "receiverDelete";
			qp.value = "N";
			qp.dataType = 1;
			qp.logicType = 1;
			qpVe.add(qp);
			if (relationId != null && !relationId.equals("")) {
				qp = new QueryParameterCarrier();
				qp.objectName = objName;
				qp.propertyName = "relationId";
				qp.value = "b.relationId=" + relationId;
				qp.dataType = 6;
				qp.logicType = 10;
				qpVe.add(qp);
			}
		} else {
			qp = new QueryParameterCarrier();
			qp.objectName = objName;
			qp.propertyName = "senderId";
			qp.value = String.valueOf(ub.getStaffer().getId());
			qp.dataType = 3;
			qp.logicType = 1;
			qpVe.add(qp);
			qp = new QueryParameterCarrier();
			qp.objectName = objName;
			qp.propertyName = "senderDelete";
			qp.value = "N";
			qp.dataType = 1;
			qp.logicType = 1;
			qpVe.add(qp);
		}

		if (searchType != null && !searchType.equals("")) {
			if (searchType.equals("2")) {
				qp = new QueryParameterCarrier();
				qp.objectName = objName;
				qp.propertyName = "title";
				qp.value = ttl;
				qp.dataType = 1;
				qp.logicType = 9;
				qpVe.add(qp);
			} else {
				qParas = request.getParameter("cb_sen_date");
				p1 = request.getParameter("cb_ttl");
				String oParas = request.getParameter("cb_con");
				String currentPage = request.getParameter("cb_sen");
				if (qParas != null && qParas.equals("1")) {
					qpVe.add(new QueryParameterCarrier(objName, "sendTime",
							startDate, 4, 5));
					qpVe.add(new QueryParameterCarrier(objName, "sendTime",
							EncCommonHelper.getNextDateStr(endDate,
									"yyyy-MM-dd"), 4, 2));
					parameters.put("cbSenDate", qParas);
				}

				if (p1 != null && p1.equals("1")) {
					qpVe.add(new QueryParameterCarrier(objName, "title", ttl,
							1, 9));
					parameters.put("cbTtl", p1);
				}

				if (oParas != null && oParas.equals("1")) {
					qpVe.add(new QueryParameterCarrier(objName, "content", con,
							1, 9));
					parameters.put("cbCon", oParas);
				}

				if (currentPage != null && currentPage.equals("1")) {
					qpVe.add(new QueryParameterCarrier(objName, "senderName",
							sen, 1, 9));
					parameters.put("cbSen", currentPage);
				}
			}
		}

		parameters.put("ttl", ttl);
		parameters.put("con", con);
		parameters.put("sen", sen);
		parameters.put("startDate", startDate);
		parameters.put("endDate", endDate);
		QueryParameterCarrier[] arg24 = new QueryParameterCarrier[qpVe.size()];

		for (int arg25 = 0; arg25 < qpVe.size(); ++arg25) {
			arg24[arg25] = (QueryParameterCarrier) qpVe.get(arg25);
		}

		OrderParameterCarrier arg26 = new OrderParameterCarrier();
		arg26.objectName = objName;
		arg26.propertyName = "sendTime";
		arg26.orderTarget = "desc";
		OrderParameterCarrier[] arg27 = new OrderParameterCarrier[]{arg26};
		int arg28 = EncCommonHelper.getCurrentPageIndex(request);
		QueryResult qr = this.queryDao.doQuery(arg28,
				EncCommonHelper.getPageSize(request), arg24, arg27,
				(String[]) null, shortNameMap, retObjs, (String[]) null);
		mv.addObject("qr", qr);
		mv.addAllObjects(parameters);
		mv.addObject("searchType", searchType);
		mv.addObject("type", type);
		mv.addObject("relationId", relationId);
		return mv;
	}

	public ModelAndView handleInfo(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		ModelAndView mv = new ModelAndView(this.infoView);
		mv.addObject("message",
				this.messageDao.get(Message.class, Long.parseLong(id)));
		mv.addObject("receiverList",
				this.messageDao.getReceiverList(Long.parseLong(id)));
		mv.addObject("accList", this.accessoryDao.getList("MESSAGE",
				Long.parseLong(id), (String[]) null, 0L));
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		this.messageDao.updateReadFlag(Long.parseLong(id), ub.getStaffer()
				.getId());
		return mv;
	}

	public ModelAndView handleDelete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		String type = request.getParameter("type");
		ModelAndView mv = new ModelAndView(this.operateSuccessView);
		String[] idArr = id.split(";");
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);

		for (int j = 0; j < idArr.length; ++j) {
			Message m = (Message) this.messageDao.get(Message.class,
					Long.parseLong(idArr[j]));
			if (m != null) {
				List receiverList = this.messageDao.getReceiverList(m.getId());
				boolean receiversDelete = true;

				for (int i = 0; i < receiverList.size(); ++i) {
					MessageReceiver r = (MessageReceiver) receiverList.get(i);
					if (type != null && type.equals("IN")
							&& r.getReceiverDelete().equals("N")
							&& r.getReceiverId() == ub.getStaffer().getId()) {
						r.setReceiverDelete("Y");
						this.messageDao.update(r);
					}

					if (r.getReceiverDelete().equals("N")) {
						receiversDelete = false;
					}
				}

				if (type != null && type.equals("IN")) {
					if (m.getSenderDelete().equals("Y") && receiversDelete) {
						this.messageDao.delete(m.getId());
						this.messageDao.deleteReceivers(m.getId());
						this.accessoryDao.delete("MESSAGE", m.getId());
					}
				} else if (receiversDelete) {
					this.messageDao.delete(m.getId());
					this.messageDao.deleteReceivers(m.getId());
					this.accessoryDao.delete("MESSAGE", m.getId());
				} else {
					m.setSenderDelete("Y");
					this.messageDao.update(m);
				}
			}

			EncCommonHelper.setAlertMessage(request, "删除成功");
			mv.addObject("returnValue", "1");
		}

		return mv;
	}

	public ModelAndView handleMain(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.mainView);
		String defaultUrl = request.getParameter("default_url");
		if (defaultUrl != null && !defaultUrl.equals("")) {
			mv.addObject("defaultUrl", defaultUrl);
		}

		return mv;
	}

	public ModelAndView handleMenu(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.menuView);
		return mv;
	}

	public ModelAndView handleNewMessageCount(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		int count = 0;
		if (ub != null) {
			count = this.messageDao.getNewMessageCount(ub.getStaffer().getId());
		}

		response.setContentType("text/plain");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		ServletOutputStream out = response.getOutputStream();
		out.write(String.valueOf(count).getBytes());
		out.flush();
		out.close();
		return null;
	}

	public ModelAndView handleAppGetMessCoun(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String stafId = request.getParameter("staf_id");
		int msgCoun = 0;
		String lastMsgTitl = "";
		long lastMsgId = 0L;
		if (stafId != null && !stafId.equals("")) {
			msgCoun = this.messageDao
					.getNewMessageCount(Long.parseLong(stafId));
			List map = this.messageDao.getTopInList(Long.parseLong(stafId), 1);
			if (!map.isEmpty()) {
				Message mapper = (Message) ((Object[]) map.get(0))[0];
				lastMsgTitl = mapper.getTitle();
				lastMsgId = mapper.getId();
			}
		}

		HashMap map1 = new HashMap();
		map1.put("msgCoun", Integer.valueOf(msgCoun));
		map1.put("lastMsgId", Long.valueOf(lastMsgId));
		map1.put("lastMsgTitl", lastMsgTitl);
		map1.put("verCode", EncCommonHelper.getProperty("appVerCode"));
		map1.put("sysCode", EncCommonHelper.getProperty("sysCode"));
		response.setContentType("text/plain");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		ObjectMapper mapper1 = new ObjectMapper();
		String json = mapper1.writeValueAsString(map1);
		ServletOutputStream out = response.getOutputStream();
		out.write(json.getBytes("utf-8"));
		out.flush();
		out.close();
		return null;
	}

	public ModelAndView handleAppMessList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		HashMap map = new HashMap();
		UserStatus s = EncInterceptorHelper.checkAppUserSignon(request);
		UserBean ub = s.getUserBean();
		if (ub == null) {
			map.put("status", "NO_LOGIN");
			map.put("errMsg", "");
		} else {
			try {
				String mapper = request.getParameter("show_prev");
				int json = this.messageDao.getAppListCount(ub.getStaffer()
						.getId());
				int out = EncCommonHelper.getPageSize(request);
				int pageno = EncCommonHelper.getCurrentPageIndex(request, json,
						out);
				if (json > pageno * out) {
					map.put("more", "Y");
				} else {
					map.put("more", "N");
				}

				List list;
				if (mapper != null && mapper.equals("Y")) {
					list = this.messageDao.getAppList(ub.getStaffer().getId(),
							0, pageno * out);
				} else {
					list = this.messageDao.getAppList(ub.getStaffer().getId(),
							(pageno - 1) * out, out);
				}

				SimpleDateFormat sfl = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				ArrayList items = new ArrayList();

				for (int i = 0; i < list.size(); ++i) {
					Object[] objs = (Object[]) list.get(i);
					Message msg = (Message) objs[0];
					String readFlag = (String) objs[1];
					String cont = msg.getContent();
					if (cont != null && cont.length() > 50) {
						cont = cont.substring(0, 50) + "...";
					}

					HashMap item = new HashMap();
					item.put("id", Long.valueOf(msg.getId()));
					item.put("titl", msg.getTitle());
					item.put("creaName", msg.getSenderName());
					item.put("creaTime", sfl.format(msg.getSendTime()));
					item.put("cont", cont);
					item.put("readFlag", readFlag);
					items.add(item);
				}

				map.put("items", items);
				map.put("status", "OK");
				map.put("errMsg", "");
			} catch (Exception arg18) {
				map.put("status", "ERROR");
				map.put("errMsg", arg18.toString());
			}
		}

		response.setContentType("text/plain");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		ObjectMapper arg19 = new ObjectMapper();
		String arg20 = arg19.writeValueAsString(map);
		ServletOutputStream arg21 = response.getOutputStream();
		arg21.write(arg20.getBytes("utf-8"));
		arg21.flush();
		arg21.close();
		return null;
	}

	public ModelAndView handleAppMessInfo(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		HashMap map = new HashMap();
		UserStatus s = EncInterceptorHelper.checkAppUserSignon(request);
		UserBean ub = s.getUserBean();
		if (ub == null) {
			map.put("status", "NO_LOGIN");
			map.put("errMsg", "");
		} else {
			try {
				long mapper = Long.parseLong(request.getParameter("id"));
				String out = request.getParameter("pars_cont");
				Message msg = (Message) this.messageDao.get(Message.class,
						mapper);
				if (msg != null) {
					this.messageDao.updateReadFlag(msg.getId(), ub.getStaffer()
							.getId());
					SimpleDateFormat sfl = new SimpleDateFormat(
							"yyyy-MM-dd HH:mm");
					map.put("titl", msg.getTitle());
					if (out != null && out.equals("Y")) {
						String cont = msg.getContent();
						cont = cont.replace(">", "&gt;");
						cont = cont.replace("<", "&lt;");
						cont = cont.replace("\n", "<br>");
						map.put("cont", cont);
					} else {
						map.put("cont", msg.getContent());
					}

					map.put("creaName", msg.getSenderName());
					map.put("creaId", Long.valueOf(msg.getSenderId()));
					map.put("creaTime", sfl.format(msg.getSendTime()));
					map.put("receName", msg.getReceiverName());
				}

				map.put("status", "OK");
				map.put("errMsg", "");
			} catch (Exception arg11) {
				map.put("status", "ERROR");
				map.put("errMsg", arg11.toString());
			}
		}

		response.setContentType("text/plain");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		ObjectMapper mapper1 = new ObjectMapper();
		String json = mapper1.writeValueAsString(map);
		ServletOutputStream out1 = response.getOutputStream();
		out1.write(json.getBytes("utf-8"));
		out1.flush();
		out1.close();
		return null;
	}

	public ModelAndView handleAppSetMessRead(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		HashMap map = new HashMap();
		UserStatus s = EncInterceptorHelper.checkAppUserSignon(request);
		UserBean ub = s.getUserBean();
		if (ub == null) {
			map.put("status", "NO_LOGIN");
			map.put("errMsg", "");
		} else {
			try {
				this.messageDao.updateReadFlag(ub.getStaffer().getId());
				map.put("status", "OK");
				map.put("errMsg", "");
			} catch (Exception arg8) {
				map.put("status", "ERROR");
				map.put("errMsg", arg8.toString());
			}
		}

		response.setContentType("text/plain");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(map);
		ServletOutputStream out = response.getOutputStream();
		out.write(json.getBytes("utf-8"));
		out.flush();
		out.close();
		return null;
	}

	public ModelAndView handleAppMessNew(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		HashMap map = new HashMap();
		UserStatus s = EncInterceptorHelper.checkAppUserSignon(request);
		UserBean ub = s.getUserBean();
		String json;
		if (ub == null) {
			map.put("status", "NO_LOGIN");
			map.put("errMsg", "");
		} else {
			try {
				String mapper = request.getParameter("id_str");
				json = request.getParameter("name_str");
				String out = request.getParameter("cont");
				String titl = request.getParameter("titl");
				String[] idArr = mapper.split(";");
				String[] nameArr = json.split(";");
				ArrayList sendedArr = new ArrayList();
				ArrayList arr = new ArrayList();

				for (int receiverName = 0; receiverName < idArr.length; ++receiverName) {
					if (!sendedArr.contains(idArr[receiverName])) {
						MessageReceiver obj = new MessageReceiver();
						obj.setReceiverId(Long.parseLong(idArr[receiverName]));
						obj.setReceiverName(nameArr[receiverName]);
						arr.add(obj);
						sendedArr.add(idArr[receiverName]);
					}
				}

				String arg22 = "";
				if (arr.size() > 0) {
					arg22 = ((MessageReceiver) arr.get(0)).getReceiverName();
					if (arr.size() > 1) {
						arg22 = arg22 + "...";
					}
				}

				Message arg23 = new Message();
				arg23.setContent(out);
				arg23.setTitle(titl);
				arg23.setId(this.idGenerator.getId(Message.class.getName()));
				arg23.setSenderId(ub.getStaffer().getId());
				arg23.setSenderName(ub.getStaffer().getStafferName());
				arg23.setSendTime(new Date());
				arg23.setReceiverName(arg22);
				arg23.setHasAttach("N");
				this.messageDao.save(arg23);
				long curId = this.idGenerator.getId(
						MessageReceiver.class.getName(), arr.size());

				for (int i = 0; i < arr.size(); ++i) {
					MessageReceiver r = (MessageReceiver) arr.get(i);
					r.setId(curId);
					r.setMessageId(arg23.getId());
					this.messageDao.save(r);
					++curId;
				}

				map.put("status", "OK");
				map.put("errMsg", "");
			} catch (Exception arg19) {
				map.put("status", "ERROR");
				map.put("errMsg", arg19.toString());
			}
		}

		response.setContentType("text/plain");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		ObjectMapper arg20 = new ObjectMapper();
		json = arg20.writeValueAsString(map);
		ServletOutputStream arg21 = response.getOutputStream();
		arg21.write(json.getBytes("utf-8"));
		arg21.flush();
		arg21.close();
		return null;
	}

	public ModelAndView handleAppMessDele(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		HashMap map = new HashMap();
		UserStatus s = EncInterceptorHelper.checkAppUserSignon(request);
		UserBean ub = s.getUserBean();
		if (ub == null) {
			map.put("status", "NO_LOGIN");
			map.put("errMsg", "");
		} else {
			try {
				String mapper = request.getParameter("id");
				Message json = (Message) this.messageDao.get(Message.class,
						Long.parseLong(mapper));
				if (json != null) {
					List out = this.messageDao.getReceiverList(json.getId());
					boolean receiversDelete = true;

					for (int i = 0; i < out.size(); ++i) {
						MessageReceiver r = (MessageReceiver) out.get(i);
						if (r.getReceiverDelete().equals("N")
								&& r.getReceiverId() == ub.getStaffer().getId()) {
							r.setReceiverDelete("Y");
							this.messageDao.update(r);
						}

						if (r.getReceiverDelete().equals("N")) {
							receiversDelete = false;
						}
					}

					if (json.getSenderDelete().equals("Y") && receiversDelete) {
						this.messageDao.delete(json.getId());
						this.messageDao.deleteReceivers(json.getId());
						this.accessoryDao.delete("MESSAGE", json.getId());
					}
				}

				map.put("status", "OK");
				map.put("errMsg", "");
			} catch (Exception arg11) {
				map.put("status", "ERROR");
				map.put("errMsg", arg11.toString());
			}
		}

		response.setContentType("text/plain");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		ObjectMapper arg12 = new ObjectMapper();
		String arg13 = arg12.writeValueAsString(map);
		ServletOutputStream arg14 = response.getOutputStream();
		arg14.write(arg13.getBytes("utf-8"));
		arg14.flush();
		arg14.close();
		return null;
	}

	public String getInfoView() {
		return this.infoView;
	}

	public void setInfoView(String infoView) {
		this.infoView = infoView;
	}

	public String getListView() {
		return this.listView;
	}

	public void setListView(String listView) {
		this.listView = listView;
	}

	public MessageDao getMessageDao() {
		return this.messageDao;
	}

	public void setMessageDao(MessageDao messageDao) {
		this.messageDao = messageDao;
	}

	public String getOperateSuccessView() {
		return this.operateSuccessView;
	}

	public void setOperateSuccessView(String operateSuccessView) {
		this.operateSuccessView = operateSuccessView;
	}

	public QueryDao getQueryDao() {
		return this.queryDao;
	}

	public void setQueryDao(QueryDao queryDao) {
		this.queryDao = queryDao;
	}

	public AccessoryDao getAccessoryDao() {
		return this.accessoryDao;
	}

	public void setAccessoryDao(AccessoryDao accessoryDao) {
		this.accessoryDao = accessoryDao;
	}
}